//
//  TFButtonView.swift
//  TalkfunSDKDemo
//
//  Created by 邓盛明 on 2022/2/16.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit
import CloudKit

class TFButtonView: UIView {
    @objc public var chatBtn:UIButton!
    @objc public var askBtn:UIButton!
    @objc public var selectView:UIView!
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.chatBtn = UIButton.init()
        self.chatBtn.setTitle("互动区", for: .normal)
        self.chatBtn.setTitleColor(UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 1.0), for: .normal)
        self.chatBtn.titleLabel?.font = UIFont.PingFang(size: 16)
        self.chatBtn.isHidden = true
        addSubview(self.chatBtn)
        
        self.askBtn = UIButton.init()
        self.askBtn.isHidden = true
        self.askBtn.setTitle("提问", for: .normal)
        self.askBtn.setTitleColor(UIColor(red: 20/255.0, green: 23/255.0, blue: 26/255.0, alpha: 1.0), for: .normal)
        self.askBtn.titleLabel?.font = UIFont.PingFang(size: 15)
        addSubview(self.askBtn)
  
        self.selectView = UIView.init()
        self.selectView.backgroundColor = UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 1.0)
        addSubview(self.selectView)
     
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        if (self.chatBtn.isHidden && self.askBtn.isHidden == false) {
            self.askBtn.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)
          
            self.selectView.isHidden = false
           
            self.selectView.frame = CGRect(x: (self.frame.size.width-20)/2.0, y: self.frame.size.height-4, width: 20, height: 4)
        } else if (self.askBtn.isHidden && self.chatBtn.isHidden == false) {
            self.chatBtn.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)
            self.selectView.isHidden = false
            self.selectView.frame = CGRect(x: (self.frame.size.width-20)/2.0, y: self.frame.size.height-4, width: 20, height: 4)
        } else if (self.askBtn.isHidden == false && self.chatBtn.isHidden == false) {
            
            self.chatBtn.frame = CGRect(x: 0, y: 0, width: self.frame.size.width/2, height: self.frame.size.height)
            self.askBtn.frame = CGRect(x: self.frame.size.width/2, y: 0, width: self.frame.size.width/2, height: self.frame.size.height)
            self.selectView.isHidden = false
            self.selectView.frame = CGRect(x: (self.frame.size.width/2.0-20)/2.0, y: self.frame.size.height-4, width: 20, height: 4)
           
        } else if (self.askBtn.isHidden && self.chatBtn.isHidden) {
            self.selectView.isHidden = true
        }
        
    }
    
    @objc public func selectButton(_ btn:UIButton) {
        if (btn == self.chatBtn) {
            self.selectView.frame = CGRect(x: (self.frame.size.width/2.0-20)/2.0, y: self.frame.size.height-4, width: 20, height: 4)
            self.askBtn.setTitleColor(UIColor(red: 20/255.0, green: 23/255.0, blue: 26/255.0, alpha: 1.0), for: .normal)
            self.chatBtn.setTitleColor(UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 1.0), for: .normal)
        } else {
            self.selectView.frame = CGRect(x: self.frame.size.width/2.0 + (self.frame.size.width/2.0-20)/2.0, y: self.frame.size.height-4, width: 20, height: 4)
            self.chatBtn.setTitleColor(UIColor(red: 20/255.0, green: 23/255.0, blue: 26/255.0, alpha: 1.0), for: .normal)
            self.askBtn.setTitleColor(UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 1.0), for: .normal)
        }
    }
    
    @objc public func buttonsAddTarget(_ target:NSObject, action:Selector) {
        
        self.chatBtn .addTarget(target, action: action, for: .touchUpInside)
        self.askBtn .addTarget(target, action: action, for: .touchUpInside)
      }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
